<!-- 文章管理 -->
<template>
  <div class=''>
    
    <el-row :gutter="20">
      <el-col :span="4">
        <el-input class="ml-10" v-model="id" placeholder="搜索ID">
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-input class="ml-10" v-model="userid" placeholder="搜索用户ID">
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-select v-model="status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statues" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-button class="ml-10" icon="el-icon-search" circle :loading="isSearch" @click="getDocuments()"></el-button>

      </el-col>
      
    </el-row>


    <el-table :data="documents.records" style="width: 100%" v-loading="isSearch" @row-dblclick="(row, column, event)=>{$commons.toWindodBlog(row.id)}">
      <el-table-column label="ID" width="80">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="背景" width="60">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="50" :src="scope.row.background"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.userid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="60">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ $commons.map(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阅读量" width="80">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.views }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" width="200">
        <!-- <template slot-scope="scope">
          <span
            style="margin-left: 10px">{{scope.row.title}}</span>
        </template> -->
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" :content="scope.row.title" placement="top">
            <span style="margin-left: 10px">{{ $commons.overflowHidForText(scope.row.title,10) }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ $commons.myDate(scope.row.updatetime,1) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="简介" width="300">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" :content="scope.row.slogan" placement="top">
            <span style="margin-left: 10px">{{ $commons.overflowHidForText(scope.row.slogan,18) }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link v-if="scope.row.status != 'DRAFT'" icon="el-icon-close" slot="reference" @click="under(scope.row)"></el-link>
            <el-popconfirm title="删除后不可恢复，确定要删除吗？" @confirm="remove(scope.row)">
                <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
              <el-link icon="el-icon-delete" slot="reference"></el-link>
            </el-popconfirm>
              <el-link :icon="isAsync == scope.row.id?'el-icon-loading':'el-icon-refresh'" slot="reference" @click="asyncDoc(scope.row.id)"></el-link>
          <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-area mb-30 mt-20 wow fadeInUp animated">
      <nav aria-label="Page navigation example">
        <el-pagination style="float:left;" layout="prev, pager, next" :total="documents.total" :current-page="documents.current"
          @current-change="pageChange">
        </el-pagination>
      </nav>
    </div>
  </div>
</template>

<script>
  //这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
  //例如：import 《组件名称》 from '《组件路径》';

  export default {
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      //这里存放数据
      return {
        re: {
          vedio: 'http://dev.kingyin.club:9000/cloud/2022225/短视频/1640999005_1645868970708.mp4',
          slogan: '这个博主还在思考，到底该写什么才能引起你的注意。',
          nickname: '违规昵称',
          avater: 'http://dev.kingyin.club:9000/cloud/pic/littleghost.png'
        },
        isOp: false,
        dialogFormVisible: false,
        currentIndex: -1,
        documents: {
          total: 0,
          size: 10,
          current: 1,
          records: []
        },
        isSearch: false,
        id: '',
        status:'',
        userid:'',
        nickname: '',
        role: '',
        titled: '',
        statues: [{
          label: '发布',
          value: 'RELEASE'
        }, {
          label: '下架',
          value: 'DRAFT'
        }],
        roles: [{
          label: '博主',
          value: 'USER'
        }, {
          label: '管理员',
          value: 'ADMIN'
        }, {
          label: '超级用户',
          value: 'ROOT'
        }],
        isAsync:-1,
        isUpdate: false
      };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
      async under(data) {
        this.isSearch = true;
        const pram = {
          status: 'UNDERFRAME',
          id: data.id
        }
        var res = await this.$api.core.setDocumentStatus(pram);
        
        this.isSearch = false
      },
      async asyncDoc(id) {
        this.isAsync = id;
        var ids = [];
        ids.push(id);
        const pram = {
          ids:[id]
        }
       var res = await this.$api.core.async(pram);
       if (res.code == 20000) {
         Notiflix.Notify.Info('同步成功ID：'+res.data.success+' 失败：'+res.data.failed);
       }
       this.isAsync = -1;
      },
      async remove(data) {
        this.isSearch = true;
        const pram = {
          id: data.id
        }
        var res = await this.$api.core.removeDocument(pram);
        this.isSearch = false;
      },
      toWindow(row, column, event) {
        console.log(row);
        $commons.toWindodBlog(column.id)
      },
      async update() {
        this.isUpdate = true;
        if (this.titled == '') {
          this.users.records[this.currentIndex].titled = 0;
        } else {
          var date = new Date(this.titled);
           this.users.records[this.currentIndex].titled = date.getTime();
        }
        this.users.records[this.currentIndex].password = 'MANAGE';
        this.users.records[this.currentIndex].unicode = this.users.records[this.currentIndex].vedio;
        await this.$api.user.update(this.users.records[this.currentIndex]);
        this.isUpdate = false;
      },
      async getVedio(user) {
        const pram = {
          id: user.id
        }
        var res = await this.$api.manage.vedio(pram);
        user.vedio = res.data.path;
      },
      async handleEdit(index, user) {
        // 下架
        if (user.vedio == undefined) {
          await this.getVedio(user);
        }
        // console.log(user.titled);
        if (user.titled != 0)
          this.titled = this.$commons.timestampToTime(user.titled);
        this.currentIndex = index;
        this.dialogFormVisible = true;
      },
      handleDelete(index, user) {

      },
      pageChange(page) {
        this.documents.current = page;
        this.getDocuments(true);
      },
      async getDocuments(page=false) {
        this.isSearch = true;
        const pram = {
          page: {
            current: page?this.documents.current:1,
            size: this.documents.size
          },
          document: {
            id: this.$commons.isEmptyString(this.id) ? null : this.id,
            userid: this.$commons.isEmptyString(this.userid) ? null : this.userid,
            status: this.$commons.isEmptyString(this.status) ? null : this.status,
          }
        }
        var res = await this.$api.manage.documents(pram);
        this.documents = res.data;
        this.isSearch = false;
      }
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {

    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      this.getDocuments();
    },
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  }

</script>
<style lang='scss' scoped>
  //@import url(); 引入公共css类

</style>
